/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Server;

/**
 *
 * @author spok
 */
import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import data.levels.*;
import java.awt.Point;
import plar.Elements.Player;
import plar.Key;
import plar.Mouse;
import plar.UserInput;



public class Servlet extends HttpServlet {

    
  public Server server;
  public int i;
  
  public void init(ServletConfig config) throws ServletException
  {
    super.init(config);
    server = new Server(new Level1());
  }
  
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

      HttpSession session = request.getSession(true);
      Player sessionPlayer = (Player)session.getValue("plar0.player");
      
      PrintWriter out = response.getWriter();
        
      String action = request.getParameter("action");
      String user = request.getParameter("user");
      String password = request.getParameter("password");
      String keysString =  request.getParameter("keys");
      String mouseString =  request.getParameter("mouse");
      String mouseX =  request.getParameter("mouseX");
      String mouseY =  request.getParameter("mouseY");
      String filename = request.getParameter("resource");
      if(action==null) action = "";
      if(user==null) user = "";
      if(password==null) password = "";
      if(keysString ==null) keysString  = "";
      if(mouseString ==null) mouseString  = "";
     if( mouseX ==null)  mouseX = "";
      if( mouseY ==null)  mouseY= "";
      if(  filename ==null)  filename= "";
      
      if(action.equals("info")){
            out.write(""+server.info());
        }else if(action.equals("login")){
            out.write(""+server.login(user,password));
            session.putValue("plar0.playername",request.getParameter("user"));
            session.putValue("plar0.player",new Player(request.getParameter("user")));
        }else if(action.equals("resources")){
             out.write(""+server.getResources());
        }else if(action.equals("input")){
            
            /* DIO CHE CASINO */
            
            
            //Vector <Key> keys = new Vector <Key> ();
            
            Key keys = new Key();
            
                    
            int intKeys;
            String [] keysChar = keysString.split(",");
            for(String k: keysChar)
            {

                if(k.equals("u")) keys.up=true;
                if(k.equals("d")) keys.down=true;
                if(k.equals("l")) keys.left=true;
                if(k.equals("r")) keys.right=true;
                if(k.equals("f")) keys.fire=true;
                if(k.equals("j")) keys.jump=true;
                if(k.equals("s")) keys.special=true;
                
            }
            
            Mouse mouse=new Mouse();
            String [] mouseChar = mouseString.split(",");
            
            for(String k: mouseChar)
            {

                if(k.equals("l")) mouse.left=true;
                if(k.equals("r")) mouse.right=true;
                if(k.equals("m")) mouse.middle=true;
                
            }     
            
            Point mousePos = new 
                    Point(
                        Integer.parseInt(mouseX),
                        Integer.parseInt(mouseY)
                    );
            UserInput userInput = new UserInput(keys,mouse);
            
            
            out.write("" + server.movePlayer(sessionPlayer, userInput));
        }else if(action.equals("output")){
            out.write(""+server.getState(sessionPlayer));
        }else if(action.equals("get")){
         
         
     
      File  file = new File ("../../resources/"+filename);


      FileInputStream file_input = new FileInputStream (file);
      DataInputStream data_in    = new DataInputStream (file_input );

      while (true) {
        try {
         
         out.write(data_in.readInt());

        }
        catch (EOFException eof) {
          
          break;
        }
      }
        }else if(action.equals("exit")){
            
        }else if(action.equals("admin")){
            
        }
            
            
         

  
        


    }
}
